package com.hnevc.sqlitedemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


    Button mBtnCreateDB;
    Button mBtnInsert;
    Button mBtnUpdate;
    Button mBtnDelete;
    Button mBtnQueryAll;
    TextView mTvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //初始化事件
        initEvent();
   }

    private void initEvent() {
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 修改记录
                MySQLiteOpenHelper  mySQLiteOpenHelper
                        = new MySQLiteOpenHelper(MainActivity.this,"news.db",null,1);

                SQLiteDatabase writeabledb = mySQLiteOpenHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                values.put("publishDate",sdf.format(Calendar.getInstance().getTime()));
                writeabledb.update("news", values,"id=?",new String[]{"1"});
                writeabledb.close();
            }
        });
        mBtnCreateDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 创建数据库
                //创建对象是不会马上穿创建数据库
                MySQLiteOpenHelper  mySQLiteOpenHelper
                        = new MySQLiteOpenHelper(MainActivity.this,"news.db",null,1);
                //获取可写数据库对象后才会创建数据库
                SQLiteDatabase writeabledb = mySQLiteOpenHelper.getWritableDatabase();
                Toast.makeText(MainActivity.this, "创建数据库成功", Toast.LENGTH_SHORT).show();
                writeabledb.close();
            }
        });

        mBtnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 添加一条记录
                MySQLiteOpenHelper  mySQLiteOpenHelper
                        = new MySQLiteOpenHelper(MainActivity.this,"news.db",null,1);

                SQLiteDatabase writeabledb = mySQLiteOpenHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("title","标题1");
                values.put("author","张三");
                values.put("content","今天的天气真好");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                values.put("publishDate", sdf.format(Calendar.getInstance().getTime()));
                writeabledb.insert("news",null,values);
                writeabledb.close();
            }
        });
        mBtnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 数据删除起点
                MySQLiteOpenHelper helper
                        = new MySQLiteOpenHelper(MainActivity.this, "news.db",null,1);
                SQLiteDatabase db = helper.getWritableDatabase();
                int delCount = db.delete("news","id>?",new String[]{"1"});
                Toast.makeText(MainActivity.this, delCount+"", Toast.LENGTH_SHORT).show();
                db.close();
            }
        });
        mBtnQueryAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 查询所有的数据并显示

                MySQLiteOpenHelper helper
                        = new MySQLiteOpenHelper(MainActivity.this,"news.db",null,1);
                SQLiteDatabase db = helper.getReadableDatabase();
                Cursor cursor = db.query("news",null,null,null,null,null,null);

                String message="";
                while(cursor.moveToNext()){
                    message += cursor.getInt(0)+'\t';
                    message += cursor.getString(1)+'\t';
                    message += cursor.getString(2)+'\t';
                    message += cursor.getString(3)+'\t';
                    message += cursor.getString(4)+'\n';
                }
                mTvMessage.setText(message);
                db.close();
            }
        });
    }

    private void initView() {
        mBtnCreateDB = (Button) findViewById(R.id.id_btn_createdb);
        mBtnInsert = (Button) findViewById(R.id.id_btn_insert);
        mBtnUpdate = (Button) findViewById(R.id.id_btn_update);
        mBtnDelete = (Button) findViewById(R.id.id_btn_delete);
        mBtnQueryAll = (Button) findViewById(R.id.id_btn_queryall);
        mTvMessage = (TextView) findViewById(R.id.id_tv_message);
    }
}
